package com.snda.sc.utils;

import java.util.Map;
import java.util.Map.Entry;

public class ToStringHelper {

	private StringBuilder sb = new StringBuilder();
	private boolean emptyProperty = true;

	public ToStringHelper(Class<?> clazz) {
		sb.append(clazz.getSimpleName() + ":{");
	}

	public ToStringHelper(Object obj) {
		this(obj.getClass());
	}

	public ToStringHelper append(Object name, Object value) {
		if (name == null) return this;
		emptyProperty = false;
		if (value == null) value = "null";
		sb.append(name + "=" + value + ",");
		return this;
	}

	public ToStringHelper appendAll(Map<?, ?> props) {
		for (Entry<?, ?> entry : props.entrySet()) {
			append(entry.getKey(), entry.getValue());
		}
		return this;
	}

	@Override
	public String toString() {
		if (!emptyProperty) sb = sb.deleteCharAt(sb.length() - 1);
		sb.append("}");
		return sb.toString();
	}
}
